package com.toKnow.controller;

import com.toKnow.model.vo.BasicVo;
import com.toKnow.service.CreatorService;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/api/creator/data")
public class CreatorDataController {
    @Autowired
    CreatorService creatorService;
    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }
    @ResponseBody
    @RequestMapping("/article/read")
    public BasicVo getDataArticleRead(HttpSession session) {
        Integer authorId = getUid(session);
        if(authorId == null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true, null, creatorService.getDataArticleRead(authorId));
    }
    @ResponseBody
    @RequestMapping("/article/like")
    public BasicVo getDateArticleLike(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataArticleLike(authorId));
    }
    @ResponseBody
    @RequestMapping("/article/collect")
    public BasicVo getDateArticleCollect(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataArticleCollect(authorId));
    }
    @ResponseBody
    @RequestMapping("/article/comment")
    public BasicVo getDateArticleComment(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataArticleComment(authorId));
    }
    @ResponseBody
    @RequestMapping("/source/download")
    public BasicVo getDataSourceDownload(HttpSession session) {
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataSourceDownload(authorId));
    }
    @ResponseBody
    @RequestMapping("/source/like")
    public BasicVo getDateSourceLike(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataSourceLike(authorId));
    }
    @ResponseBody
    @RequestMapping("/source/collect")
    public BasicVo getDateSourceCollect(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataSourceCollect(authorId));
    }
    @ResponseBody
    @RequestMapping("/source/comment")
    public BasicVo getDateSourceComment(HttpSession session){
        Integer authorId=getUid(session);
        if (authorId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataSourceComment(authorId));
    }

    @ResponseBody
    @RequestMapping("/fans")
    public BasicVo getDateFans(HttpSession session){
        Integer userId=getUid(session);
        if (userId==null) return new BasicVo(false,"没有找到用户名可能是没有登录",null);
        return new BasicVo(true,null, creatorService.getDataFans(userId));
    }
}
